import { inject, Injectable } from '@angular/core';
import { Observable } from 'rxjs';

// import { MENU_TOKEN } from '@config/menu';
import { Menu } from '@core/services/types';
import { BaseHttpService } from '@services/base-http.service';

import { UserInfo } from '../../store/common-store/userInfo.service';
// import { MenusService } from '@services/system/menus.service';

export interface UserLogin {
  username: string;
  password: string;
  prefix: string;
}

export interface TokenResult {
  token: string;
  refreshtoken: string;
}

@Injectable({
  providedIn: 'root'
})
export class LoginService {
  http = inject(BaseHttpService);
  authenticateIndex: string = 'authenticate';
  // private menus = inject(MENU_TOKEN);

  public login(params: UserLogin): Observable<TokenResult[]> {
    params.prefix = 'student';
    return this.http.post<TokenResult[]>(`/${this.authenticateIndex}/login?username=${params.username}&prefix=${params.prefix}`, params.password, { needSuccessInfo: false });
  }

  public getAuthInfo(): Observable<UserInfo[]> {
    return this.http.get<UserInfo[]>(`/${this.authenticateIndex}/authinfo`, {});
  }

  public getMenuByUserId(userId: number): Observable<Menu[]> {
    // 如果是静态菜单，就把下面注释放开
    // return of(this.menus);
    return this.http.get(`/systemMenu/GetAll?@filter='eq(UserId,${userId})'`, { '@relationships': 'all' });
  }
}
